const { Service } = require("uni-cloud-router");
const uniID = require('uni-id');
const response=require("../utils/response.js");
const db = uniCloud.database({
	spaceId: 'b33db069-91a2-42ff-a831-31fbd5a04b97'
});
const dbCmd = db.command;
const collection=db.collection("opendb-mall-categories");
module.exports = class CateService extends Service {
  async add(data){
  	const res=await collection.add(data);
		if(res.id){
			return response.success(res);
		}else{
			return response.fail('添加失败');
		}
  }
	async edit(id,data){
		const res=await collection.doc(id).update(data);
		if(res.updated>0){
			return response.success(res);
		}else{
			return response.fail('修改失败');
		}
	}
	async getOne(id){
		const res=await collection.doc(id).get();
		if(res.data.length>0){
			return response.success(res.data[0]);
		}else{
			return response.fail('添加失败');
		}
	}
  async list(data){
		const where={}
		if(data.is_hot!=undefined){
			where.is_hot_show=data.is_hot
		}
		if(data.is_index!=undefined){
			where.is_index_show=data.is_index
		}
		if(data.pid!=undefined){
			where.parent_id=data.pid
		}
  	const res=await collection.where(where).get();
  	if(res.affectedDocs>=0){
  		return response.success(res.data);
  	}else{
  		return response.fail('查询失败');
  	}
  }
  async delete(ids){
  	return await collection.where({
			a: dbCmd.in(ids)
		}).remove();
  }
};